package com.atguigu.tingshu.order.handle;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

//redisson接收端接收消息
@Component
public class RedisDelayHandle {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;

    @PostConstruct
    public void listener(){
        new Thread(()->{
            //一直监听
            while (true){

                try {
                    RBlockingDeque<String> blockingDeque =
                            redissonClient.getBlockingDeque(MqConst.EXCHANGE_CANCEL_ORDER);
                    String id = blockingDeque.take();
                    if (StringUtils.hasText(id)){
                        //取消订单
                        orderInfoService.orderCancel(Long.parseLong(id));
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

}
